package exa.unicen.edu.ringo.rmodel.builder.xstream.converter.helper;

import java.util.Enumeration;

import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import exa.unicen.edu.ringo.rmodel.bean.Configuration;
import exa.unicen.edu.ringo.rmodel.bean.Node;

public class NodesConverterHelper implements IConverterHelper {

	public static String NAME = "nodes";
	
	@Override
	public String name() { return NAME; }
	
	@Override
	public void marshal(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
		Configuration configuration = (Configuration) parent;
		
		for(Enumeration<Node> e = configuration.nodes.elements(); e.hasMoreElements(); ){
			Node node = e.nextElement();
			writer.startNode("node");
			context.convertAnother(node);
			writer.endNode();
		}

	}
	
	@Override
	public void unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context, Object parent) {
		Configuration configuration = (Configuration) parent;
		
		while (reader.hasMoreChildren()) {
			reader.moveDown();
			Node node = (Node) context.convertAnother(parent, Node.class);
			configuration.nodes.put(node.id, node);
			reader.moveUp();
		}
	}



}
